package com.iwith.dongmoon.util;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.view.Gravity;
import android.widget.Toast;

import com.google.gson.Gson;
import com.iwith.dongmoon.gsonlist.LoginInfo;

public class SessionManager
{

	Context mContext = null;
	SharedPreferences mPreferences = null;

	String mUserid;
	String mPassword;

	public SessionManager(Context context)
	{
		mContext = context;
		mPreferences = context.getSharedPreferences("kr.co.thot135", Activity.MODE_PRIVATE);
		mUserid=null;
		mPassword=null;
	}

	public boolean isAutoLoginSet()
	{
		return mPreferences.getBoolean("isAutoLogin", false);
	}

	public String getUserid()
	{
		return mPreferences.getString("userid", "");
	}

	public String getPassword()
	{
		return mPreferences.getString("password", "");
	}

	public void saveUserInfo(boolean isAutoLogin, String userid, String password)
	{
		SharedPreferences.Editor editor = mPreferences.edit();

		editor.putBoolean("isAutoLogin", isAutoLogin);
		editor.putString("userid", userid);
		editor.putString("password", EncryptionManager.SHA1(password));
		editor.commit();
	}

	public boolean isLogOn()
	{
		return mPreferences.getBoolean("isLogOn", false);
	}
	public void setIsLogOn(boolean value)
	{
		SharedPreferences.Editor editor = mPreferences.edit();
		editor.putBoolean("isLogOn", value);
		editor.commit();
	}

	public void logout(){
		SharedPreferences.Editor editor = mPreferences.edit();
		editor.putBoolean("isLogOn", false);
		editor.putBoolean("isAutoLogin", false);
		editor.putString("userid", "");
		editor.putString("password", "");
		editor.commit();
	}
	
	public boolean login()
	{
		return login(true, null, null);
	}

	public boolean login(boolean requestAutoLogin, String userid, String password)
	{
		SharedPreferences.Editor editor = mPreferences.edit();
		
		if (requestAutoLogin == true && isAutoLoginSet() == true)
		{

			Toast toast = Toast.makeText(mContext, "자동로그인 시도중입니다.", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.BOTTOM, 0, 35);
			toast.show();

			mUserid = mPreferences.getString("userid", "");
			mPassword = mPreferences.getString("password", "");

		} else
		{
			if (userid != null && password != null)
			{
				mUserid = userid;
				mPassword = EncryptionManager.SHA1(password);
			} else
			{
				editor.putBoolean("isLogOn", false);
				return false;
			}
		}
		
		RequestData request = new RequestData(mContext, "/login", "POST", "");
		request.addParameter("userid", mUserid);
		request.addParameter("password", mPassword);
		

		String result = request.getDataPost();

		Gson gson = new Gson();
		LoginInfo code = gson.fromJson(result, LoginInfo.class);

		if (code.getResultCode() == ResultCode.SUCCESS)
		{
			Toast toast = Toast.makeText(mContext, "로그인이 성공하였습니다.", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.BOTTOM, 0, 35);
			toast.show();

			editor = mPreferences.edit();
			editor.putBoolean("isLogOn", true);
			editor.putString("userid", mUserid);
			editor.putString("password", mPassword);
			editor.commit();
			
			return true;
		} else
		{
			Toast toast = Toast.makeText(mContext, "로그인에 실패하였습니다.", Toast.LENGTH_SHORT);
			toast.setGravity(Gravity.BOTTOM, 0, 35);
			toast.show();
			
			editor.putBoolean("isLogOn", false);
			editor.putString("userid", "");
			editor.putString("password", "");
			editor.commit();
		}

		return false;
	}
}
